{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from lightgbm.sklearn import LGBMClassifier\n",
    "from sklearn.metrics import accuracy_score, auc, roc_auc_score\n",
    "from sklearn.preprocessing import LabelEncoder\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.model_selection import KFold\n",
    "## 0. 打印设置\n",
    "pd.set_option('display.max_columns', None)\n",
    "# pd.set_option('display.max_rows', None)  ## 显示全部结果，不带省略点\n",
    "# pd.set_option('display.width', 1000)\n",
    "pd.set_option('display.float_format', '{:.0f}'.format)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "## 1.1 读取数据\n",
    "train_Base = pd.read_csv(r\"data/train.csv\")\n",
    "test_Base = pd.read_csv(r\"data/test.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>policy_id</th>\n",
       "      <th>age</th>\n",
       "      <th>customer_months</th>\n",
       "      <th>policy_bind_date</th>\n",
       "      <th>policy_state</th>\n",
       "      <th>policy_csl</th>\n",
       "      <th>policy_deductable</th>\n",
       "      <th>policy_annual_premium</th>\n",
       "      <th>umbrella_limit</th>\n",
       "      <th>insured_zip</th>\n",
       "      <th>insured_sex</th>\n",
       "      <th>insured_education_level</th>\n",
       "      <th>insured_occupation</th>\n",
       "      <th>insured_hobbies</th>\n",
       "      <th>insured_relationship</th>\n",
       "      <th>capital-gains</th>\n",
       "      <th>capital-loss</th>\n",
       "      <th>incident_date</th>\n",
       "      <th>incident_type</th>\n",
       "      <th>collision_type</th>\n",
       "      <th>incident_severity</th>\n",
       "      <th>authorities_contacted</th>\n",
       "      <th>incident_state</th>\n",
       "      <th>incident_city</th>\n",
       "      <th>incident_hour_of_the_day</th>\n",
       "      <th>number_of_vehicles_involved</th>\n",
       "      <th>property_damage</th>\n",
       "      <th>bodily_injuries</th>\n",
       "      <th>witnesses</th>\n",
       "      <th>police_report_available</th>\n",
       "      <th>total_claim_amount</th>\n",
       "      <th>injury_claim</th>\n",
       "      <th>property_claim</th>\n",
       "      <th>vehicle_claim</th>\n",
       "      <th>auto_make</th>\n",
       "      <th>auto_model</th>\n",
       "      <th>auto_year</th>\n",
       "      <th>fraud</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>681822</td>\n",
       "      <td>60</td>\n",
       "      <td>473</td>\n",
       "      <td>2002-12-17</td>\n",
       "      <td>B</td>\n",
       "      <td>500/1000</td>\n",
       "      <td>1000</td>\n",
       "      <td>1135</td>\n",
       "      <td>0</td>\n",
       "      <td>445975</td>\n",
       "      <td>FEMALE</td>\n",
       "      <td>MD</td>\n",
       "      <td>exec-managerial</td>\n",
       "      <td>camping</td>\n",
       "      <td>other-relative</td>\n",
       "      <td>0</td>\n",
       "      <td>-44262</td>\n",
       "      <td>2015-01-31</td>\n",
       "      <td>Multi-vehicle Collision</td>\n",
       "      <td>Rear Collision</td>\n",
       "      <td>Total Loss</td>\n",
       "      <td>Other</td>\n",
       "      <td>S2</td>\n",
       "      <td>Arlington</td>\n",
       "      <td>9</td>\n",
       "      <td>2</td>\n",
       "      <td>?</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>?</td>\n",
       "      <td>53253</td>\n",
       "      <td>5212</td>\n",
       "      <td>10251</td>\n",
       "      <td>39503</td>\n",
       "      <td>Saab</td>\n",
       "      <td>95</td>\n",
       "      <td>2006</td>\n",
       "      <td>nan</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>301288</td>\n",
       "      <td>36</td>\n",
       "      <td>173</td>\n",
       "      <td>1994-01-15</td>\n",
       "      <td>B</td>\n",
       "      <td>100/300</td>\n",
       "      <td>1000</td>\n",
       "      <td>916</td>\n",
       "      <td>0</td>\n",
       "      <td>469238</td>\n",
       "      <td>FEMALE</td>\n",
       "      <td>Masters</td>\n",
       "      <td>exec-managerial</td>\n",
       "      <td>camping</td>\n",
       "      <td>other-relative</td>\n",
       "      <td>0</td>\n",
       "      <td>-38591</td>\n",
       "      <td>2015-01-04</td>\n",
       "      <td>Multi-vehicle Collision</td>\n",
       "      <td>Rear Collision</td>\n",
       "      <td>Major Damage</td>\n",
       "      <td>Ambulance</td>\n",
       "      <td>S5</td>\n",
       "      <td>Springfield</td>\n",
       "      <td>22</td>\n",
       "      <td>3</td>\n",
       "      <td>?</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>NO</td>\n",
       "      <td>69401</td>\n",
       "      <td>8309</td>\n",
       "      <td>8439</td>\n",
       "      <td>50012</td>\n",
       "      <td>Mercedes</td>\n",
       "      <td>ML350</td>\n",
       "      <td>2008</td>\n",
       "      <td>nan</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>212001</td>\n",
       "      <td>36</td>\n",
       "      <td>147</td>\n",
       "      <td>1995-12-19</td>\n",
       "      <td>B</td>\n",
       "      <td>500/1000</td>\n",
       "      <td>1000</td>\n",
       "      <td>1176</td>\n",
       "      <td>5000000</td>\n",
       "      <td>595953</td>\n",
       "      <td>FEMALE</td>\n",
       "      <td>MD</td>\n",
       "      <td>adm-clerical</td>\n",
       "      <td>hiking</td>\n",
       "      <td>own-child</td>\n",
       "      <td>56753</td>\n",
       "      <td>0</td>\n",
       "      <td>2015-02-09</td>\n",
       "      <td>Single Vehicle Collision</td>\n",
       "      <td>Side Collision</td>\n",
       "      <td>Total Loss</td>\n",
       "      <td>Police</td>\n",
       "      <td>S6</td>\n",
       "      <td>Northbrook</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>NO</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>NO</td>\n",
       "      <td>63919</td>\n",
       "      <td>5572</td>\n",
       "      <td>11477</td>\n",
       "      <td>42801</td>\n",
       "      <td>Dodge</td>\n",
       "      <td>Neon</td>\n",
       "      <td>2009</td>\n",
       "      <td>nan</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>797680</td>\n",
       "      <td>24</td>\n",
       "      <td>71</td>\n",
       "      <td>1992-06-20</td>\n",
       "      <td>C</td>\n",
       "      <td>500/1000</td>\n",
       "      <td>500</td>\n",
       "      <td>1472</td>\n",
       "      <td>0</td>\n",
       "      <td>613103</td>\n",
       "      <td>FEMALE</td>\n",
       "      <td>High School</td>\n",
       "      <td>armed-forces</td>\n",
       "      <td>yachting</td>\n",
       "      <td>other-relative</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>2015-01-15</td>\n",
       "      <td>Multi-vehicle Collision</td>\n",
       "      <td>Rear Collision</td>\n",
       "      <td>Minor Damage</td>\n",
       "      <td>Police</td>\n",
       "      <td>S1</td>\n",
       "      <td>Springfield</td>\n",
       "      <td>23</td>\n",
       "      <td>3</td>\n",
       "      <td>?</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>NO</td>\n",
       "      <td>63173</td>\n",
       "      <td>12027</td>\n",
       "      <td>6500</td>\n",
       "      <td>43423</td>\n",
       "      <td>Dodge</td>\n",
       "      <td>RAM</td>\n",
       "      <td>2012</td>\n",
       "      <td>nan</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>789334</td>\n",
       "      <td>39</td>\n",
       "      <td>230</td>\n",
       "      <td>1996-11-28</td>\n",
       "      <td>C</td>\n",
       "      <td>250/500</td>\n",
       "      <td>1000</td>\n",
       "      <td>1159</td>\n",
       "      <td>4000000</td>\n",
       "      <td>581581</td>\n",
       "      <td>FEMALE</td>\n",
       "      <td>Masters</td>\n",
       "      <td>exec-managerial</td>\n",
       "      <td>reading</td>\n",
       "      <td>wife</td>\n",
       "      <td>0</td>\n",
       "      <td>-62877</td>\n",
       "      <td>2015-01-08</td>\n",
       "      <td>Parked Car</td>\n",
       "      <td>?</td>\n",
       "      <td>Minor Damage</td>\n",
       "      <td>Police</td>\n",
       "      <td>S1</td>\n",
       "      <td>Springfield</td>\n",
       "      <td>17</td>\n",
       "      <td>1</td>\n",
       "      <td>YES</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>?</td>\n",
       "      <td>8847</td>\n",
       "      <td>904</td>\n",
       "      <td>1786</td>\n",
       "      <td>6138</td>\n",
       "      <td>Accura</td>\n",
       "      <td>RSX</td>\n",
       "      <td>2003</td>\n",
       "      <td>nan</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>695</th>\n",
       "      <td>1008425</td>\n",
       "      <td>37</td>\n",
       "      <td>196</td>\n",
       "      <td>1997-06-29</td>\n",
       "      <td>C</td>\n",
       "      <td>250/500</td>\n",
       "      <td>500</td>\n",
       "      <td>1301</td>\n",
       "      <td>0</td>\n",
       "      <td>474615</td>\n",
       "      <td>MALE</td>\n",
       "      <td>JD</td>\n",
       "      <td>tech-support</td>\n",
       "      <td>video-games</td>\n",
       "      <td>wife</td>\n",
       "      <td>47627</td>\n",
       "      <td>0</td>\n",
       "      <td>2015-01-18</td>\n",
       "      <td>Single Vehicle Collision</td>\n",
       "      <td>Front Collision</td>\n",
       "      <td>Major Damage</td>\n",
       "      <td>Ambulance</td>\n",
       "      <td>S5</td>\n",
       "      <td>Columbus</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>?</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>NO</td>\n",
       "      <td>61433</td>\n",
       "      <td>10436</td>\n",
       "      <td>11432</td>\n",
       "      <td>39745</td>\n",
       "      <td>Nissan</td>\n",
       "      <td>Pathfinder</td>\n",
       "      <td>2011</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>696</th>\n",
       "      <td>770702</td>\n",
       "      <td>43</td>\n",
       "      <td>229</td>\n",
       "      <td>2001-05-29</td>\n",
       "      <td>A</td>\n",
       "      <td>250/500</td>\n",
       "      <td>500</td>\n",
       "      <td>1435</td>\n",
       "      <td>8000000</td>\n",
       "      <td>444476</td>\n",
       "      <td>MALE</td>\n",
       "      <td>College</td>\n",
       "      <td>machine-op-inspct</td>\n",
       "      <td>golf</td>\n",
       "      <td>husband</td>\n",
       "      <td>0</td>\n",
       "      <td>-32289</td>\n",
       "      <td>2015-01-13</td>\n",
       "      <td>Multi-vehicle Collision</td>\n",
       "      <td>Rear Collision</td>\n",
       "      <td>Major Damage</td>\n",
       "      <td>Ambulance</td>\n",
       "      <td>S1</td>\n",
       "      <td>Arlington</td>\n",
       "      <td>17</td>\n",
       "      <td>3</td>\n",
       "      <td>NO</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>?</td>\n",
       "      <td>68623</td>\n",
       "      <td>6798</td>\n",
       "      <td>14557</td>\n",
       "      <td>50606</td>\n",
       "      <td>Volkswagen</td>\n",
       "      <td>Passat</td>\n",
       "      <td>2013</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>697</th>\n",
       "      <td>755099</td>\n",
       "      <td>35</td>\n",
       "      <td>209</td>\n",
       "      <td>2003-01-11</td>\n",
       "      <td>C</td>\n",
       "      <td>100/300</td>\n",
       "      <td>500</td>\n",
       "      <td>1639</td>\n",
       "      <td>0</td>\n",
       "      <td>639608</td>\n",
       "      <td>FEMALE</td>\n",
       "      <td>College</td>\n",
       "      <td>transport-moving</td>\n",
       "      <td>golf</td>\n",
       "      <td>not-in-family</td>\n",
       "      <td>0</td>\n",
       "      <td>-40797</td>\n",
       "      <td>2015-03-05</td>\n",
       "      <td>Multi-vehicle Collision</td>\n",
       "      <td>Rear Collision</td>\n",
       "      <td>Minor Damage</td>\n",
       "      <td>Fire</td>\n",
       "      <td>S2</td>\n",
       "      <td>Riverwood</td>\n",
       "      <td>7</td>\n",
       "      <td>3</td>\n",
       "      <td>NO</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>YES</td>\n",
       "      <td>58033</td>\n",
       "      <td>9129</td>\n",
       "      <td>4598</td>\n",
       "      <td>40740</td>\n",
       "      <td>Mercedes</td>\n",
       "      <td>C300</td>\n",
       "      <td>2002</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>698</th>\n",
       "      <td>693804</td>\n",
       "      <td>44</td>\n",
       "      <td>275</td>\n",
       "      <td>2003-07-22</td>\n",
       "      <td>B</td>\n",
       "      <td>500/1000</td>\n",
       "      <td>2000</td>\n",
       "      <td>1042</td>\n",
       "      <td>0</td>\n",
       "      <td>432061</td>\n",
       "      <td>FEMALE</td>\n",
       "      <td>Associate</td>\n",
       "      <td>machine-op-inspct</td>\n",
       "      <td>paintball</td>\n",
       "      <td>other-relative</td>\n",
       "      <td>46822</td>\n",
       "      <td>0</td>\n",
       "      <td>2015-01-09</td>\n",
       "      <td>Multi-vehicle Collision</td>\n",
       "      <td>Rear Collision</td>\n",
       "      <td>Major Damage</td>\n",
       "      <td>Ambulance</td>\n",
       "      <td>S5</td>\n",
       "      <td>Northbend</td>\n",
       "      <td>20</td>\n",
       "      <td>3</td>\n",
       "      <td>?</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>NO</td>\n",
       "      <td>35253</td>\n",
       "      <td>7359</td>\n",
       "      <td>3464</td>\n",
       "      <td>24677</td>\n",
       "      <td>Audi</td>\n",
       "      <td>A3</td>\n",
       "      <td>2007</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>699</th>\n",
       "      <td>598086</td>\n",
       "      <td>47</td>\n",
       "      <td>263</td>\n",
       "      <td>1996-08-15</td>\n",
       "      <td>C</td>\n",
       "      <td>500/1000</td>\n",
       "      <td>500</td>\n",
       "      <td>1283</td>\n",
       "      <td>0</td>\n",
       "      <td>433809</td>\n",
       "      <td>FEMALE</td>\n",
       "      <td>High School</td>\n",
       "      <td>machine-op-inspct</td>\n",
       "      <td>sleeping</td>\n",
       "      <td>wife</td>\n",
       "      <td>54087</td>\n",
       "      <td>-61343</td>\n",
       "      <td>2015-01-08</td>\n",
       "      <td>Multi-vehicle Collision</td>\n",
       "      <td>Side Collision</td>\n",
       "      <td>Total Loss</td>\n",
       "      <td>Police</td>\n",
       "      <td>S4</td>\n",
       "      <td>Hillsdale</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>?</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>NO</td>\n",
       "      <td>24320</td>\n",
       "      <td>2250</td>\n",
       "      <td>4285</td>\n",
       "      <td>18092</td>\n",
       "      <td>Suburu</td>\n",
       "      <td>Forrestor</td>\n",
       "      <td>2008</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1000 rows × 38 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     policy_id  age  customer_months policy_bind_date policy_state policy_csl  \\\n",
       "0       681822   60              473       2002-12-17            B   500/1000   \n",
       "1       301288   36              173       1994-01-15            B    100/300   \n",
       "2       212001   36              147       1995-12-19            B   500/1000   \n",
       "3       797680   24               71       1992-06-20            C   500/1000   \n",
       "4       789334   39              230       1996-11-28            C    250/500   \n",
       "..         ...  ...              ...              ...          ...        ...   \n",
       "695    1008425   37              196       1997-06-29            C    250/500   \n",
       "696     770702   43              229       2001-05-29            A    250/500   \n",
       "697     755099   35              209       2003-01-11            C    100/300   \n",
       "698     693804   44              275       2003-07-22            B   500/1000   \n",
       "699     598086   47              263       1996-08-15            C   500/1000   \n",
       "\n",
       "     policy_deductable  policy_annual_premium  umbrella_limit  insured_zip  \\\n",
       "0                 1000                   1135               0       445975   \n",
       "1                 1000                    916               0       469238   \n",
       "2                 1000                   1176         5000000       595953   \n",
       "3                  500                   1472               0       613103   \n",
       "4                 1000                   1159         4000000       581581   \n",
       "..                 ...                    ...             ...          ...   \n",
       "695                500                   1301               0       474615   \n",
       "696                500                   1435         8000000       444476   \n",
       "697                500                   1639               0       639608   \n",
       "698               2000                   1042               0       432061   \n",
       "699                500                   1283               0       433809   \n",
       "\n",
       "    insured_sex insured_education_level insured_occupation insured_hobbies  \\\n",
       "0        FEMALE                      MD    exec-managerial         camping   \n",
       "1        FEMALE                 Masters    exec-managerial         camping   \n",
       "2        FEMALE                      MD       adm-clerical          hiking   \n",
       "3        FEMALE             High School       armed-forces        yachting   \n",
       "4        FEMALE                 Masters    exec-managerial         reading   \n",
       "..          ...                     ...                ...             ...   \n",
       "695        MALE                      JD       tech-support     video-games   \n",
       "696        MALE                 College  machine-op-inspct            golf   \n",
       "697      FEMALE                 College   transport-moving            golf   \n",
       "698      FEMALE               Associate  machine-op-inspct       paintball   \n",
       "699      FEMALE             High School  machine-op-inspct        sleeping   \n",
       "\n",
       "    insured_relationship  capital-gains  capital-loss incident_date  \\\n",
       "0         other-relative              0        -44262    2015-01-31   \n",
       "1         other-relative              0        -38591    2015-01-04   \n",
       "2              own-child          56753             0    2015-02-09   \n",
       "3         other-relative              0             0    2015-01-15   \n",
       "4                   wife              0        -62877    2015-01-08   \n",
       "..                   ...            ...           ...           ...   \n",
       "695                 wife          47627             0    2015-01-18   \n",
       "696              husband              0        -32289    2015-01-13   \n",
       "697        not-in-family              0        -40797    2015-03-05   \n",
       "698       other-relative          46822             0    2015-01-09   \n",
       "699                 wife          54087        -61343    2015-01-08   \n",
       "\n",
       "                incident_type   collision_type incident_severity  \\\n",
       "0     Multi-vehicle Collision   Rear Collision        Total Loss   \n",
       "1     Multi-vehicle Collision   Rear Collision      Major Damage   \n",
       "2    Single Vehicle Collision   Side Collision        Total Loss   \n",
       "3     Multi-vehicle Collision   Rear Collision      Minor Damage   \n",
       "4                  Parked Car                ?      Minor Damage   \n",
       "..                        ...              ...               ...   \n",
       "695  Single Vehicle Collision  Front Collision      Major Damage   \n",
       "696   Multi-vehicle Collision   Rear Collision      Major Damage   \n",
       "697   Multi-vehicle Collision   Rear Collision      Minor Damage   \n",
       "698   Multi-vehicle Collision   Rear Collision      Major Damage   \n",
       "699   Multi-vehicle Collision   Side Collision        Total Loss   \n",
       "\n",
       "    authorities_contacted incident_state incident_city  \\\n",
       "0                   Other             S2     Arlington   \n",
       "1               Ambulance             S5   Springfield   \n",
       "2                  Police             S6    Northbrook   \n",
       "3                  Police             S1   Springfield   \n",
       "4                  Police             S1   Springfield   \n",
       "..                    ...            ...           ...   \n",
       "695             Ambulance             S5      Columbus   \n",
       "696             Ambulance             S1     Arlington   \n",
       "697                  Fire             S2     Riverwood   \n",
       "698             Ambulance             S5     Northbend   \n",
       "699                Police             S4     Hillsdale   \n",
       "\n",
       "     incident_hour_of_the_day  number_of_vehicles_involved property_damage  \\\n",
       "0                           9                            2               ?   \n",
       "1                          22                            3               ?   \n",
       "2                           0                            1              NO   \n",
       "3                          23                            3               ?   \n",
       "4                          17                            1             YES   \n",
       "..                        ...                          ...             ...   \n",
       "695                         4                            1               ?   \n",
       "696                        17                            3              NO   \n",
       "697                         7                            3              NO   \n",
       "698                        20                            3               ?   \n",
       "699                         5                            3               ?   \n",
       "\n",
       "     bodily_injuries  witnesses police_report_available  total_claim_amount  \\\n",
       "0                  0          3                       ?               53253   \n",
       "1                  0          0                      NO               69401   \n",
       "2                  2          0                      NO               63919   \n",
       "3                  0          0                      NO               63173   \n",
       "4                  0          0                       ?                8847   \n",
       "..               ...        ...                     ...                 ...   \n",
       "695                0          3                      NO               61433   \n",
       "696                0          1                       ?               68623   \n",
       "697                2          0                     YES               58033   \n",
       "698                1          0                      NO               35253   \n",
       "699                0          0                      NO               24320   \n",
       "\n",
       "     injury_claim  property_claim  vehicle_claim   auto_make  auto_model  \\\n",
       "0            5212           10251          39503        Saab          95   \n",
       "1            8309            8439          50012    Mercedes       ML350   \n",
       "2            5572           11477          42801       Dodge        Neon   \n",
       "3           12027            6500          43423       Dodge         RAM   \n",
       "4             904            1786           6138      Accura         RSX   \n",
       "..            ...             ...            ...         ...         ...   \n",
       "695         10436           11432          39745      Nissan  Pathfinder   \n",
       "696          6798           14557          50606  Volkswagen      Passat   \n",
       "697          9129            4598          40740    Mercedes        C300   \n",
       "698          7359            3464          24677        Audi          A3   \n",
       "699          2250            4285          18092      Suburu   Forrestor   \n",
       "\n",
       "     auto_year  fraud  \n",
       "0         2006    nan  \n",
       "1         2008    nan  \n",
       "2         2009    nan  \n",
       "3         2012    nan  \n",
       "4         2003    nan  \n",
       "..         ...    ...  \n",
       "695       2011      1  \n",
       "696       2013      1  \n",
       "697       2002      0  \n",
       "698       2007      1  \n",
       "699       2008      0  \n",
       "\n",
       "[1000 rows x 38 columns]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## 1.2 数据合并\n",
    "data = pd.concat([test_Base, train_Base], axis=0)\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "## 1.3 数据清洗\n",
    "## 1.3.1 索引完善\n",
    "data.index = range(len(data))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "policy_id                        0\n",
       "age                              0\n",
       "customer_months                  0\n",
       "policy_bind_date                 0\n",
       "policy_state                     0\n",
       "policy_csl                       0\n",
       "policy_deductable                0\n",
       "policy_annual_premium            0\n",
       "umbrella_limit                   0\n",
       "insured_zip                      0\n",
       "insured_sex                      0\n",
       "insured_education_level          0\n",
       "insured_occupation               0\n",
       "insured_hobbies                  0\n",
       "insured_relationship             0\n",
       "capital-gains                    0\n",
       "capital-loss                     0\n",
       "incident_date                    0\n",
       "incident_type                    0\n",
       "collision_type                   0\n",
       "incident_severity                0\n",
       "authorities_contacted            0\n",
       "incident_state                   0\n",
       "incident_city                    0\n",
       "incident_hour_of_the_day         0\n",
       "number_of_vehicles_involved      0\n",
       "property_damage                  0\n",
       "bodily_injuries                  0\n",
       "witnesses                        0\n",
       "police_report_available          0\n",
       "total_claim_amount               0\n",
       "injury_claim                     0\n",
       "property_claim                   0\n",
       "vehicle_claim                    0\n",
       "auto_make                        0\n",
       "auto_model                       0\n",
       "auto_year                        0\n",
       "fraud                          300\n",
       "dtype: int64"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## 1.4 数据探索\n",
    "## 1.4.1 空值数量\n",
    "data.isnull().sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "policy_id 1000\n",
      "age 45\n",
      "customer_months 385\n",
      "policy_bind_date 955\n",
      "policy_state 3\n",
      "policy_csl 3\n",
      "policy_deductable 3\n",
      "policy_annual_premium 996\n",
      "umbrella_limit 11\n",
      "insured_zip 999\n",
      "insured_sex 2\n",
      "insured_education_level 7\n",
      "insured_occupation 14\n",
      "insured_hobbies 20\n",
      "insured_relationship 6\n",
      "capital-gains 490\n",
      "capital-loss 525\n",
      "incident_date 113\n",
      "incident_type 4\n",
      "collision_type 4\n",
      "incident_severity 4\n",
      "authorities_contacted 5\n",
      "incident_state 7\n",
      "incident_city 7\n",
      "incident_hour_of_the_day 24\n",
      "number_of_vehicles_involved 4\n",
      "property_damage 3\n",
      "bodily_injuries 3\n",
      "witnesses 4\n",
      "police_report_available 3\n",
      "total_claim_amount 989\n",
      "injury_claim 945\n",
      "property_claim 931\n",
      "vehicle_claim 991\n",
      "auto_make 14\n",
      "auto_model 39\n",
      "auto_year 21\n",
      "fraud 2\n"
     ]
    }
   ],
   "source": [
    "# 1.4.2 唯一值个数\n",
    "for col in data.columns:\n",
    "    print(col, data[col].nunique())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>col_name</th>\n",
       "      <th>value</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>policy_bind_date</td>\n",
       "      <td>955</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>incident_date</td>\n",
       "      <td>113</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>auto_model</td>\n",
       "      <td>39</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>insured_hobbies</td>\n",
       "      <td>20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>insured_occupation</td>\n",
       "      <td>14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>auto_make</td>\n",
       "      <td>14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>insured_education_level</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>incident_state</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>incident_city</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>insured_relationship</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>authorities_contacted</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>collision_type</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>incident_severity</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>incident_type</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>policy_csl</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>policy_state</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>property_damage</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>police_report_available</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>insured_sex</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   col_name  value\n",
       "0          policy_bind_date    955\n",
       "8             incident_date    113\n",
       "18               auto_model     39\n",
       "6           insured_hobbies     20\n",
       "5        insured_occupation     14\n",
       "17                auto_make     14\n",
       "4   insured_education_level      7\n",
       "13           incident_state      7\n",
       "14            incident_city      7\n",
       "7      insured_relationship      6\n",
       "12    authorities_contacted      5\n",
       "10           collision_type      4\n",
       "11        incident_severity      4\n",
       "9             incident_type      4\n",
       "2                policy_csl      3\n",
       "1              policy_state      3\n",
       "15          property_damage      3\n",
       "16  police_report_available      3\n",
       "3               insured_sex      2"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## 1.4.3 字符串的字段，唯一值统计\n",
    "cat_columns = data.select_dtypes(include='object').columns  \n",
    "\n",
    "column_name = []\n",
    "unique_value = []\n",
    " \n",
    "for col in cat_columns:\n",
    "    column_name.append(col)\n",
    "    unique_value.append(data[col].nunique())\n",
    "\n",
    "df = pd.DataFrame()\n",
    "df['col_name'] = column_name\n",
    "df['value'] = unique_value\n",
    "df = df.sort_values('value', ascending=False)\n",
    " \n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2    343\n",
       "0    343\n",
       "1    314\n",
       "Name: police_report_available, dtype: int64"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## 2 特征工程\n",
    "## 2.0 特征编码--property_damage、police_report_available\n",
    "data['property_damage'].value_counts()\n",
    "data['property_damage'] = data['property_damage'].map({'NO': 0, 'YES': 1, '?': 2})\n",
    "data['property_damage'].value_counts()\n",
    "\n",
    "data['police_report_available'].value_counts()\n",
    "data['police_report_available'] = data['police_report_available'].map({'NO': 0, 'YES': 1, '?': 2})\n",
    "data['police_report_available'].value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>policy_id</th>\n",
       "      <th>age</th>\n",
       "      <th>customer_months</th>\n",
       "      <th>policy_state</th>\n",
       "      <th>policy_csl</th>\n",
       "      <th>policy_deductable</th>\n",
       "      <th>policy_annual_premium</th>\n",
       "      <th>umbrella_limit</th>\n",
       "      <th>insured_zip</th>\n",
       "      <th>insured_sex</th>\n",
       "      <th>insured_education_level</th>\n",
       "      <th>insured_occupation</th>\n",
       "      <th>insured_hobbies</th>\n",
       "      <th>insured_relationship</th>\n",
       "      <th>capital-gains</th>\n",
       "      <th>capital-loss</th>\n",
       "      <th>incident_type</th>\n",
       "      <th>collision_type</th>\n",
       "      <th>incident_severity</th>\n",
       "      <th>authorities_contacted</th>\n",
       "      <th>incident_state</th>\n",
       "      <th>incident_city</th>\n",
       "      <th>incident_hour_of_the_day</th>\n",
       "      <th>number_of_vehicles_involved</th>\n",
       "      <th>property_damage</th>\n",
       "      <th>bodily_injuries</th>\n",
       "      <th>witnesses</th>\n",
       "      <th>police_report_available</th>\n",
       "      <th>total_claim_amount</th>\n",
       "      <th>injury_claim</th>\n",
       "      <th>property_claim</th>\n",
       "      <th>vehicle_claim</th>\n",
       "      <th>auto_make</th>\n",
       "      <th>auto_model</th>\n",
       "      <th>auto_year</th>\n",
       "      <th>fraud</th>\n",
       "      <th>policy_bind_date_diff</th>\n",
       "      <th>incident_date_diff</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>681822</td>\n",
       "      <td>60</td>\n",
       "      <td>473</td>\n",
       "      <td>B</td>\n",
       "      <td>500/1000</td>\n",
       "      <td>1000</td>\n",
       "      <td>1135</td>\n",
       "      <td>0</td>\n",
       "      <td>445975</td>\n",
       "      <td>FEMALE</td>\n",
       "      <td>MD</td>\n",
       "      <td>exec-managerial</td>\n",
       "      <td>camping</td>\n",
       "      <td>other-relative</td>\n",
       "      <td>0</td>\n",
       "      <td>-44262</td>\n",
       "      <td>Multi-vehicle Collision</td>\n",
       "      <td>Rear Collision</td>\n",
       "      <td>Total Loss</td>\n",
       "      <td>Other</td>\n",
       "      <td>S2</td>\n",
       "      <td>Arlington</td>\n",
       "      <td>9</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>53253</td>\n",
       "      <td>5212</td>\n",
       "      <td>10251</td>\n",
       "      <td>39503</td>\n",
       "      <td>Saab</td>\n",
       "      <td>95</td>\n",
       "      <td>2006</td>\n",
       "      <td>nan</td>\n",
       "      <td>4740</td>\n",
       "      <td>9168</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>301288</td>\n",
       "      <td>36</td>\n",
       "      <td>173</td>\n",
       "      <td>B</td>\n",
       "      <td>100/300</td>\n",
       "      <td>1000</td>\n",
       "      <td>916</td>\n",
       "      <td>0</td>\n",
       "      <td>469238</td>\n",
       "      <td>FEMALE</td>\n",
       "      <td>Masters</td>\n",
       "      <td>exec-managerial</td>\n",
       "      <td>camping</td>\n",
       "      <td>other-relative</td>\n",
       "      <td>0</td>\n",
       "      <td>-38591</td>\n",
       "      <td>Multi-vehicle Collision</td>\n",
       "      <td>Rear Collision</td>\n",
       "      <td>Major Damage</td>\n",
       "      <td>Ambulance</td>\n",
       "      <td>S5</td>\n",
       "      <td>Springfield</td>\n",
       "      <td>22</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>69401</td>\n",
       "      <td>8309</td>\n",
       "      <td>8439</td>\n",
       "      <td>50012</td>\n",
       "      <td>Mercedes</td>\n",
       "      <td>ML350</td>\n",
       "      <td>2008</td>\n",
       "      <td>nan</td>\n",
       "      <td>1482</td>\n",
       "      <td>9141</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>212001</td>\n",
       "      <td>36</td>\n",
       "      <td>147</td>\n",
       "      <td>B</td>\n",
       "      <td>500/1000</td>\n",
       "      <td>1000</td>\n",
       "      <td>1176</td>\n",
       "      <td>5000000</td>\n",
       "      <td>595953</td>\n",
       "      <td>FEMALE</td>\n",
       "      <td>MD</td>\n",
       "      <td>adm-clerical</td>\n",
       "      <td>hiking</td>\n",
       "      <td>own-child</td>\n",
       "      <td>56753</td>\n",
       "      <td>0</td>\n",
       "      <td>Single Vehicle Collision</td>\n",
       "      <td>Side Collision</td>\n",
       "      <td>Total Loss</td>\n",
       "      <td>Police</td>\n",
       "      <td>S6</td>\n",
       "      <td>Northbrook</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>63919</td>\n",
       "      <td>5572</td>\n",
       "      <td>11477</td>\n",
       "      <td>42801</td>\n",
       "      <td>Dodge</td>\n",
       "      <td>Neon</td>\n",
       "      <td>2009</td>\n",
       "      <td>nan</td>\n",
       "      <td>2185</td>\n",
       "      <td>9177</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>797680</td>\n",
       "      <td>24</td>\n",
       "      <td>71</td>\n",
       "      <td>C</td>\n",
       "      <td>500/1000</td>\n",
       "      <td>500</td>\n",
       "      <td>1472</td>\n",
       "      <td>0</td>\n",
       "      <td>613103</td>\n",
       "      <td>FEMALE</td>\n",
       "      <td>High School</td>\n",
       "      <td>armed-forces</td>\n",
       "      <td>yachting</td>\n",
       "      <td>other-relative</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>Multi-vehicle Collision</td>\n",
       "      <td>Rear Collision</td>\n",
       "      <td>Minor Damage</td>\n",
       "      <td>Police</td>\n",
       "      <td>S1</td>\n",
       "      <td>Springfield</td>\n",
       "      <td>23</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>63173</td>\n",
       "      <td>12027</td>\n",
       "      <td>6500</td>\n",
       "      <td>43423</td>\n",
       "      <td>Dodge</td>\n",
       "      <td>RAM</td>\n",
       "      <td>2012</td>\n",
       "      <td>nan</td>\n",
       "      <td>908</td>\n",
       "      <td>9152</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>789334</td>\n",
       "      <td>39</td>\n",
       "      <td>230</td>\n",
       "      <td>C</td>\n",
       "      <td>250/500</td>\n",
       "      <td>1000</td>\n",
       "      <td>1159</td>\n",
       "      <td>4000000</td>\n",
       "      <td>581581</td>\n",
       "      <td>FEMALE</td>\n",
       "      <td>Masters</td>\n",
       "      <td>exec-managerial</td>\n",
       "      <td>reading</td>\n",
       "      <td>wife</td>\n",
       "      <td>0</td>\n",
       "      <td>-62877</td>\n",
       "      <td>Parked Car</td>\n",
       "      <td>?</td>\n",
       "      <td>Minor Damage</td>\n",
       "      <td>Police</td>\n",
       "      <td>S1</td>\n",
       "      <td>Springfield</td>\n",
       "      <td>17</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>8847</td>\n",
       "      <td>904</td>\n",
       "      <td>1786</td>\n",
       "      <td>6138</td>\n",
       "      <td>Accura</td>\n",
       "      <td>RSX</td>\n",
       "      <td>2003</td>\n",
       "      <td>nan</td>\n",
       "      <td>2530</td>\n",
       "      <td>9145</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>995</th>\n",
       "      <td>1008425</td>\n",
       "      <td>37</td>\n",
       "      <td>196</td>\n",
       "      <td>C</td>\n",
       "      <td>250/500</td>\n",
       "      <td>500</td>\n",
       "      <td>1301</td>\n",
       "      <td>0</td>\n",
       "      <td>474615</td>\n",
       "      <td>MALE</td>\n",
       "      <td>JD</td>\n",
       "      <td>tech-support</td>\n",
       "      <td>video-games</td>\n",
       "      <td>wife</td>\n",
       "      <td>47627</td>\n",
       "      <td>0</td>\n",
       "      <td>Single Vehicle Collision</td>\n",
       "      <td>Front Collision</td>\n",
       "      <td>Major Damage</td>\n",
       "      <td>Ambulance</td>\n",
       "      <td>S5</td>\n",
       "      <td>Columbus</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>61433</td>\n",
       "      <td>10436</td>\n",
       "      <td>11432</td>\n",
       "      <td>39745</td>\n",
       "      <td>Nissan</td>\n",
       "      <td>Pathfinder</td>\n",
       "      <td>2011</td>\n",
       "      <td>1</td>\n",
       "      <td>2743</td>\n",
       "      <td>9155</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>996</th>\n",
       "      <td>770702</td>\n",
       "      <td>43</td>\n",
       "      <td>229</td>\n",
       "      <td>A</td>\n",
       "      <td>250/500</td>\n",
       "      <td>500</td>\n",
       "      <td>1435</td>\n",
       "      <td>8000000</td>\n",
       "      <td>444476</td>\n",
       "      <td>MALE</td>\n",
       "      <td>College</td>\n",
       "      <td>machine-op-inspct</td>\n",
       "      <td>golf</td>\n",
       "      <td>husband</td>\n",
       "      <td>0</td>\n",
       "      <td>-32289</td>\n",
       "      <td>Multi-vehicle Collision</td>\n",
       "      <td>Rear Collision</td>\n",
       "      <td>Major Damage</td>\n",
       "      <td>Ambulance</td>\n",
       "      <td>S1</td>\n",
       "      <td>Arlington</td>\n",
       "      <td>17</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>68623</td>\n",
       "      <td>6798</td>\n",
       "      <td>14557</td>\n",
       "      <td>50606</td>\n",
       "      <td>Volkswagen</td>\n",
       "      <td>Passat</td>\n",
       "      <td>2013</td>\n",
       "      <td>1</td>\n",
       "      <td>4173</td>\n",
       "      <td>9150</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>997</th>\n",
       "      <td>755099</td>\n",
       "      <td>35</td>\n",
       "      <td>209</td>\n",
       "      <td>C</td>\n",
       "      <td>100/300</td>\n",
       "      <td>500</td>\n",
       "      <td>1639</td>\n",
       "      <td>0</td>\n",
       "      <td>639608</td>\n",
       "      <td>FEMALE</td>\n",
       "      <td>College</td>\n",
       "      <td>transport-moving</td>\n",
       "      <td>golf</td>\n",
       "      <td>not-in-family</td>\n",
       "      <td>0</td>\n",
       "      <td>-40797</td>\n",
       "      <td>Multi-vehicle Collision</td>\n",
       "      <td>Rear Collision</td>\n",
       "      <td>Minor Damage</td>\n",
       "      <td>Fire</td>\n",
       "      <td>S2</td>\n",
       "      <td>Riverwood</td>\n",
       "      <td>7</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>58033</td>\n",
       "      <td>9129</td>\n",
       "      <td>4598</td>\n",
       "      <td>40740</td>\n",
       "      <td>Mercedes</td>\n",
       "      <td>C300</td>\n",
       "      <td>2002</td>\n",
       "      <td>0</td>\n",
       "      <td>4765</td>\n",
       "      <td>9201</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>998</th>\n",
       "      <td>693804</td>\n",
       "      <td>44</td>\n",
       "      <td>275</td>\n",
       "      <td>B</td>\n",
       "      <td>500/1000</td>\n",
       "      <td>2000</td>\n",
       "      <td>1042</td>\n",
       "      <td>0</td>\n",
       "      <td>432061</td>\n",
       "      <td>FEMALE</td>\n",
       "      <td>Associate</td>\n",
       "      <td>machine-op-inspct</td>\n",
       "      <td>paintball</td>\n",
       "      <td>other-relative</td>\n",
       "      <td>46822</td>\n",
       "      <td>0</td>\n",
       "      <td>Multi-vehicle Collision</td>\n",
       "      <td>Rear Collision</td>\n",
       "      <td>Major Damage</td>\n",
       "      <td>Ambulance</td>\n",
       "      <td>S5</td>\n",
       "      <td>Northbend</td>\n",
       "      <td>20</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>35253</td>\n",
       "      <td>7359</td>\n",
       "      <td>3464</td>\n",
       "      <td>24677</td>\n",
       "      <td>Audi</td>\n",
       "      <td>A3</td>\n",
       "      <td>2007</td>\n",
       "      <td>1</td>\n",
       "      <td>4957</td>\n",
       "      <td>9146</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>999</th>\n",
       "      <td>598086</td>\n",
       "      <td>47</td>\n",
       "      <td>263</td>\n",
       "      <td>C</td>\n",
       "      <td>500/1000</td>\n",
       "      <td>500</td>\n",
       "      <td>1283</td>\n",
       "      <td>0</td>\n",
       "      <td>433809</td>\n",
       "      <td>FEMALE</td>\n",
       "      <td>High School</td>\n",
       "      <td>machine-op-inspct</td>\n",
       "      <td>sleeping</td>\n",
       "      <td>wife</td>\n",
       "      <td>54087</td>\n",
       "      <td>-61343</td>\n",
       "      <td>Multi-vehicle Collision</td>\n",
       "      <td>Side Collision</td>\n",
       "      <td>Total Loss</td>\n",
       "      <td>Police</td>\n",
       "      <td>S4</td>\n",
       "      <td>Hillsdale</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>24320</td>\n",
       "      <td>2250</td>\n",
       "      <td>4285</td>\n",
       "      <td>18092</td>\n",
       "      <td>Suburu</td>\n",
       "      <td>Forrestor</td>\n",
       "      <td>2008</td>\n",
       "      <td>0</td>\n",
       "      <td>2425</td>\n",
       "      <td>9145</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1000 rows × 38 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     policy_id  age  customer_months policy_state policy_csl  \\\n",
       "0       681822   60              473            B   500/1000   \n",
       "1       301288   36              173            B    100/300   \n",
       "2       212001   36              147            B   500/1000   \n",
       "3       797680   24               71            C   500/1000   \n",
       "4       789334   39              230            C    250/500   \n",
       "..         ...  ...              ...          ...        ...   \n",
       "995    1008425   37              196            C    250/500   \n",
       "996     770702   43              229            A    250/500   \n",
       "997     755099   35              209            C    100/300   \n",
       "998     693804   44              275            B   500/1000   \n",
       "999     598086   47              263            C   500/1000   \n",
       "\n",
       "     policy_deductable  policy_annual_premium  umbrella_limit  insured_zip  \\\n",
       "0                 1000                   1135               0       445975   \n",
       "1                 1000                    916               0       469238   \n",
       "2                 1000                   1176         5000000       595953   \n",
       "3                  500                   1472               0       613103   \n",
       "4                 1000                   1159         4000000       581581   \n",
       "..                 ...                    ...             ...          ...   \n",
       "995                500                   1301               0       474615   \n",
       "996                500                   1435         8000000       444476   \n",
       "997                500                   1639               0       639608   \n",
       "998               2000                   1042               0       432061   \n",
       "999                500                   1283               0       433809   \n",
       "\n",
       "    insured_sex insured_education_level insured_occupation insured_hobbies  \\\n",
       "0        FEMALE                      MD    exec-managerial         camping   \n",
       "1        FEMALE                 Masters    exec-managerial         camping   \n",
       "2        FEMALE                      MD       adm-clerical          hiking   \n",
       "3        FEMALE             High School       armed-forces        yachting   \n",
       "4        FEMALE                 Masters    exec-managerial         reading   \n",
       "..          ...                     ...                ...             ...   \n",
       "995        MALE                      JD       tech-support     video-games   \n",
       "996        MALE                 College  machine-op-inspct            golf   \n",
       "997      FEMALE                 College   transport-moving            golf   \n",
       "998      FEMALE               Associate  machine-op-inspct       paintball   \n",
       "999      FEMALE             High School  machine-op-inspct        sleeping   \n",
       "\n",
       "    insured_relationship  capital-gains  capital-loss  \\\n",
       "0         other-relative              0        -44262   \n",
       "1         other-relative              0        -38591   \n",
       "2              own-child          56753             0   \n",
       "3         other-relative              0             0   \n",
       "4                   wife              0        -62877   \n",
       "..                   ...            ...           ...   \n",
       "995                 wife          47627             0   \n",
       "996              husband              0        -32289   \n",
       "997        not-in-family              0        -40797   \n",
       "998       other-relative          46822             0   \n",
       "999                 wife          54087        -61343   \n",
       "\n",
       "                incident_type   collision_type incident_severity  \\\n",
       "0     Multi-vehicle Collision   Rear Collision        Total Loss   \n",
       "1     Multi-vehicle Collision   Rear Collision      Major Damage   \n",
       "2    Single Vehicle Collision   Side Collision        Total Loss   \n",
       "3     Multi-vehicle Collision   Rear Collision      Minor Damage   \n",
       "4                  Parked Car                ?      Minor Damage   \n",
       "..                        ...              ...               ...   \n",
       "995  Single Vehicle Collision  Front Collision      Major Damage   \n",
       "996   Multi-vehicle Collision   Rear Collision      Major Damage   \n",
       "997   Multi-vehicle Collision   Rear Collision      Minor Damage   \n",
       "998   Multi-vehicle Collision   Rear Collision      Major Damage   \n",
       "999   Multi-vehicle Collision   Side Collision        Total Loss   \n",
       "\n",
       "    authorities_contacted incident_state incident_city  \\\n",
       "0                   Other             S2     Arlington   \n",
       "1               Ambulance             S5   Springfield   \n",
       "2                  Police             S6    Northbrook   \n",
       "3                  Police             S1   Springfield   \n",
       "4                  Police             S1   Springfield   \n",
       "..                    ...            ...           ...   \n",
       "995             Ambulance             S5      Columbus   \n",
       "996             Ambulance             S1     Arlington   \n",
       "997                  Fire             S2     Riverwood   \n",
       "998             Ambulance             S5     Northbend   \n",
       "999                Police             S4     Hillsdale   \n",
       "\n",
       "     incident_hour_of_the_day  number_of_vehicles_involved  property_damage  \\\n",
       "0                           9                            2                2   \n",
       "1                          22                            3                2   \n",
       "2                           0                            1                0   \n",
       "3                          23                            3                2   \n",
       "4                          17                            1                1   \n",
       "..                        ...                          ...              ...   \n",
       "995                         4                            1                2   \n",
       "996                        17                            3                0   \n",
       "997                         7                            3                0   \n",
       "998                        20                            3                2   \n",
       "999                         5                            3                2   \n",
       "\n",
       "     bodily_injuries  witnesses  police_report_available  total_claim_amount  \\\n",
       "0                  0          3                        2               53253   \n",
       "1                  0          0                        0               69401   \n",
       "2                  2          0                        0               63919   \n",
       "3                  0          0                        0               63173   \n",
       "4                  0          0                        2                8847   \n",
       "..               ...        ...                      ...                 ...   \n",
       "995                0          3                        0               61433   \n",
       "996                0          1                        2               68623   \n",
       "997                2          0                        1               58033   \n",
       "998                1          0                        0               35253   \n",
       "999                0          0                        0               24320   \n",
       "\n",
       "     injury_claim  property_claim  vehicle_claim   auto_make  auto_model  \\\n",
       "0            5212           10251          39503        Saab          95   \n",
       "1            8309            8439          50012    Mercedes       ML350   \n",
       "2            5572           11477          42801       Dodge        Neon   \n",
       "3           12027            6500          43423       Dodge         RAM   \n",
       "4             904            1786           6138      Accura         RSX   \n",
       "..            ...             ...            ...         ...         ...   \n",
       "995         10436           11432          39745      Nissan  Pathfinder   \n",
       "996          6798           14557          50606  Volkswagen      Passat   \n",
       "997          9129            4598          40740    Mercedes        C300   \n",
       "998          7359            3464          24677        Audi          A3   \n",
       "999          2250            4285          18092      Suburu   Forrestor   \n",
       "\n",
       "     auto_year  fraud  policy_bind_date_diff  incident_date_diff  \n",
       "0         2006    nan                   4740                9168  \n",
       "1         2008    nan                   1482                9141  \n",
       "2         2009    nan                   2185                9177  \n",
       "3         2012    nan                    908                9152  \n",
       "4         2003    nan                   2530                9145  \n",
       "..         ...    ...                    ...                 ...  \n",
       "995       2011      1                   2743                9155  \n",
       "996       2013      1                   4173                9150  \n",
       "997       2002      0                   4765                9201  \n",
       "998       2007      1                   4957                9146  \n",
       "999       2008      0                   2425                9145  \n",
       "\n",
       "[1000 rows x 38 columns]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## 2.1 特征编码--日期编码\n",
    "# policy_bind_date, incident_date\n",
    "data['policy_bind_date'] = pd.to_datetime(data['policy_bind_date'])\n",
    "data['incident_date'] = pd.to_datetime(data['incident_date'])\n",
    " \n",
    "# 查看最大日期，最小日期\n",
    "data['policy_bind_date'].min() # 1990-01-08\n",
    "data['policy_bind_date'].max() # 2015-02-22\n",
    "\n",
    "data['incident_date'].min() # 2015-01-01\n",
    "data['incident_date'].max() # 2015-03-01\n",
    "\n",
    "base_date = data['policy_bind_date'].min()\n",
    "# 转换为date_diff\n",
    "data['policy_bind_date_diff'] = (data['policy_bind_date'] - base_date).dt.days\n",
    "data['incident_date_diff'] = (data['incident_date'] - base_date).dt.days\n",
    "\n",
    "#去掉原始日期字段 policy_bind_date    incident_date\n",
    "data.drop(['policy_bind_date', 'incident_date'], axis=1, inplace=True)\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['age', 'customer_months', 'policy_state', 'policy_csl',\n",
       "       'policy_deductable', 'policy_annual_premium', 'umbrella_limit',\n",
       "       'insured_zip', 'insured_sex', 'insured_education_level',\n",
       "       'insured_occupation', 'insured_hobbies', 'insured_relationship',\n",
       "       'capital-gains', 'capital-loss', 'incident_type', 'collision_type',\n",
       "       'incident_severity', 'authorities_contacted', 'incident_state',\n",
       "       'incident_city', 'incident_hour_of_the_day',\n",
       "       'number_of_vehicles_involved', 'property_damage', 'bodily_injuries',\n",
       "       'witnesses', 'police_report_available', 'total_claim_amount',\n",
       "       'injury_claim', 'property_claim', 'vehicle_claim', 'auto_make',\n",
       "       'auto_model', 'auto_year', 'fraud', 'policy_bind_date_diff',\n",
       "       'incident_date_diff'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## 2.2 去除无关的特征\n",
    "data.drop(['policy_id'], axis=1, inplace=True)\n",
    "data.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>policy_state</th>\n",
       "      <th>policy_csl</th>\n",
       "      <th>insured_sex</th>\n",
       "      <th>insured_education_level</th>\n",
       "      <th>insured_occupation</th>\n",
       "      <th>insured_hobbies</th>\n",
       "      <th>insured_relationship</th>\n",
       "      <th>incident_type</th>\n",
       "      <th>collision_type</th>\n",
       "      <th>incident_severity</th>\n",
       "      <th>authorities_contacted</th>\n",
       "      <th>incident_state</th>\n",
       "      <th>incident_city</th>\n",
       "      <th>auto_make</th>\n",
       "      <th>auto_model</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>10</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>4</td>\n",
       "      <td>6</td>\n",
       "      <td>8</td>\n",
       "      <td>24</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>10</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>27</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>19</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>6</td>\n",
       "      <td>4</td>\n",
       "      <td>30</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>15</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>6</td>\n",
       "      <td>0</td>\n",
       "      <td>31</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>995</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>12</td>\n",
       "      <td>18</td>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>9</td>\n",
       "      <td>29</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>996</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "      <td>9</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>13</td>\n",
       "      <td>28</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>997</th>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>13</td>\n",
       "      <td>9</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>8</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>998</th>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>6</td>\n",
       "      <td>13</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>999</th>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>6</td>\n",
       "      <td>17</td>\n",
       "      <td>5</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>11</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1000 rows × 15 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     policy_state  policy_csl  insured_sex  insured_education_level  \\\n",
       "0               1           2            0                        4   \n",
       "1               1           0            0                        5   \n",
       "2               1           2            0                        4   \n",
       "3               2           2            0                        2   \n",
       "4               2           1            0                        5   \n",
       "..            ...         ...          ...                      ...   \n",
       "995             2           1            1                        3   \n",
       "996             0           1            1                        1   \n",
       "997             2           0            0                        1   \n",
       "998             1           2            0                        0   \n",
       "999             2           2            0                        2   \n",
       "\n",
       "     insured_occupation  insured_hobbies  insured_relationship  incident_type  \\\n",
       "0                     3                4                     2              0   \n",
       "1                     3                4                     2              0   \n",
       "2                     0               10                     3              2   \n",
       "3                     1               19                     2              0   \n",
       "4                     3               15                     5              1   \n",
       "..                  ...              ...                   ...            ...   \n",
       "995                  12               18                     5              2   \n",
       "996                   6                9                     0              0   \n",
       "997                  13                9                     1              0   \n",
       "998                   6               13                     2              0   \n",
       "999                   6               17                     5              0   \n",
       "\n",
       "     collision_type  incident_severity  authorities_contacted  incident_state  \\\n",
       "0                 2                  2                      3               1   \n",
       "1                 2                  0                      0               4   \n",
       "2                 3                  2                      4               5   \n",
       "3                 2                  1                      4               0   \n",
       "4                 0                  1                      4               0   \n",
       "..              ...                ...                    ...             ...   \n",
       "995               1                  0                      0               4   \n",
       "996               2                  0                      0               0   \n",
       "997               2                  1                      1               1   \n",
       "998               2                  0                      0               4   \n",
       "999               3                  2                      4               3   \n",
       "\n",
       "     incident_city  auto_make  auto_model  \n",
       "0                0         10           3  \n",
       "1                6          8          24  \n",
       "2                4          4          27  \n",
       "3                6          4          30  \n",
       "4                6          0          31  \n",
       "..             ...        ...         ...  \n",
       "995              1          9          29  \n",
       "996              0         13          28  \n",
       "997              5          8           7  \n",
       "998              3          1           4  \n",
       "999              2         11          15  \n",
       "\n",
       "[1000 rows x 15 columns]"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## 2.3 标签编码\n",
    "cat_columns = data.select_dtypes(include=['object']).columns\n",
    "le = LabelEncoder()\n",
    "for col in cat_columns:\n",
    "    data[col] = le.fit_transform(data[col])\n",
    "data[cat_columns]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "## 2.4 分箱编码\n",
    "\n",
    "# ## 1）age分箱\n",
    "# for x in range(10,70,10):\n",
    "#     train_Base[train_Base['age'].between(x,x+10)].loc[:,['age']]=x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "## 3. 数据集切分\n",
    "## 3.1 切分训练集和测试集\n",
    "train = data[data['fraud'].notnull()]\n",
    "test = data[data['fraud'].isnull()]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "## 3.2 训练集中，训练集和验证集的划分\n",
    "\n",
    "# x_train, x_train_01 = train_test_split(train.drop(['fraud'],axis=1), test_size=0.2, random_state=42)  # 25% of remaining data as validation set  \n",
    "# y_train, y_train_01 = train_test_split(train['fraud'], test_size=0.2, random_state=42)  # Split labels accordingly  \n",
    "\n",
    "x_train, x_train_01, y_train, y_train_01 = train_test_split(train.drop(['fraud'],axis=1), train['fraud'], test_size=0.3, random_state=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "## 4. 模型训练\n",
    "## 4.1 建立模型\n",
    "gbm = LGBMClassifier(n_estimators=600, learning_rate=0.01, boosting_type='gbdt',  ## 模型训练超参数 调优参考：https://blog.51cto.com/u_16213313/7201851\n",
    "                     objective='binary',   ## LGBMClassifier详解： https://blog.csdn.net/yeshang_lady/article/details/118638269\n",
    "                     max_depth=-1,\n",
    "                     random_state=2022,\n",
    "                     metric='auc')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "LGBMClassifier(learning_rate=0.01, metric='auc', n_estimators=600,\n",
       "               objective='binary', random_state=2022)"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 4.2 模型训练\n",
    "## train.drop(['fraud'],axis=1) ## axis=0 表示行，axis=1 表示列\n",
    "gbm.fit(x_train, y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# 4.3 模型预测，以proba进行提交，结果会更好\n",
    "y_train_01_pred = gbm.predict_proba(x_train_01)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "auc值: 0.8622448979591836\n",
      "Accuracy: 0.8095238095238095\n",
      "Precision: 0.6666666666666666\n",
      "Recall: 0.5714285714285714\n",
      "F1 Score: 0.6153846153846153\n"
     ]
    }
   ],
   "source": [
    "## 5. 模型评估\n",
    "## 5.1 评估auc值\n",
    "auc = roc_auc_score(y_train_01, y_train_01_pred[:,-1]) # 计算auc值\n",
    "print(\"auc值:\", auc)\n",
    "\n",
    "## 5.2 概率转换\n",
    "y_train_01_pred[:, 1][y_train_01_pred[:, 1] > 0.5] = '1'\n",
    "y_train_01_pred[:, 1][y_train_01_pred[:, 1] <= 0.5] = '0'\n",
    "y_train_01_pred\n",
    "\n",
    "## 5.3 评估accuracy，precision，recall，f1\n",
    "from sklearn.metrics import precision_score, recall_score, f1_score\n",
    " \n",
    "accuracy=accuracy_score(y_train_01, y_train_01_pred[:,-1])  ## 计算准确率\n",
    "precision = precision_score(y_train_01, y_train_01_pred[:,-1]) # 计算精确率\n",
    "recall = recall_score(y_train_01, y_train_01_pred[:,-1]) # 计算召回率\n",
    "f1 = f1_score(y_train_01, y_train_01_pred[:,-1]) # 计算F1值\n",
    "\n",
    "\n",
    "# 输出计算得到的准确率、召回率和F1值\n",
    "print(\"Accuracy:\", accuracy)\n",
    "print(\"Precision:\", precision)\n",
    "print(\"Recall:\", recall)\n",
    "print(\"F1 Score:\", f1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [],
   "source": [
    "# ## 5.1 模型命名，版本控制\n",
    "# model_name='model_0_86224_base'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.96747996, 0.        ],\n",
       "       [0.21587384, 1.        ],\n",
       "       [0.98673083, 0.        ],\n",
       "       [0.98478039, 0.        ],\n",
       "       [0.96471509, 0.        ],\n",
       "       [0.9146069 , 0.        ],\n",
       "       [0.97079274, 0.        ],\n",
       "       [0.96803434, 0.        ],\n",
       "       [0.99554571, 0.        ],\n",
       "       [0.88304021, 0.        ],\n",
       "       [0.9959851 , 0.        ],\n",
       "       [0.95974966, 0.        ],\n",
       "       [0.2613624 , 1.        ],\n",
       "       [0.98518623, 0.        ],\n",
       "       [0.99145713, 0.        ],\n",
       "       [0.33889417, 1.        ],\n",
       "       [0.98244624, 0.        ],\n",
       "       [0.91168157, 0.        ],\n",
       "       [0.16629331, 1.        ],\n",
       "       [0.87491679, 0.        ],\n",
       "       [0.88442368, 0.        ],\n",
       "       [0.98119372, 0.        ],\n",
       "       [0.55177523, 0.        ],\n",
       "       [0.96880901, 0.        ],\n",
       "       [0.9932192 , 0.        ],\n",
       "       [0.47305982, 1.        ],\n",
       "       [0.9976463 , 0.        ],\n",
       "       [0.98516801, 0.        ],\n",
       "       [0.19709417, 1.        ],\n",
       "       [0.96659308, 0.        ],\n",
       "       [0.79649178, 0.        ],\n",
       "       [0.98661809, 0.        ],\n",
       "       [0.23718113, 1.        ],\n",
       "       [0.95030261, 0.        ],\n",
       "       [0.99435896, 0.        ],\n",
       "       [0.97616274, 0.        ],\n",
       "       [0.12919013, 1.        ],\n",
       "       [0.99037501, 0.        ],\n",
       "       [0.98699863, 0.        ],\n",
       "       [0.97903998, 0.        ],\n",
       "       [0.40158922, 1.        ],\n",
       "       [0.984633  , 0.        ],\n",
       "       [0.98907625, 0.        ],\n",
       "       [0.9907054 , 0.        ],\n",
       "       [0.949612  , 0.        ],\n",
       "       [0.97905688, 0.        ],\n",
       "       [0.9804799 , 0.        ],\n",
       "       [0.97292957, 0.        ],\n",
       "       [0.33841088, 1.        ],\n",
       "       [0.98988906, 0.        ],\n",
       "       [0.986005  , 0.        ],\n",
       "       [0.97473131, 0.        ],\n",
       "       [0.99553417, 0.        ],\n",
       "       [0.68993427, 0.        ],\n",
       "       [0.54317499, 0.        ],\n",
       "       [0.97174988, 0.        ],\n",
       "       [0.81807151, 0.        ],\n",
       "       [0.52129047, 0.        ],\n",
       "       [0.96333609, 0.        ],\n",
       "       [0.21535444, 1.        ],\n",
       "       [0.99507642, 0.        ],\n",
       "       [0.98838843, 0.        ],\n",
       "       [0.98246233, 0.        ],\n",
       "       [0.16045068, 1.        ],\n",
       "       [0.99302871, 0.        ],\n",
       "       [0.90722676, 0.        ],\n",
       "       [0.37293463, 1.        ],\n",
       "       [0.30241338, 1.        ],\n",
       "       [0.98963797, 0.        ],\n",
       "       [0.9870826 , 0.        ],\n",
       "       [0.99261413, 0.        ],\n",
       "       [0.95785412, 0.        ],\n",
       "       [0.28139283, 1.        ],\n",
       "       [0.27089562, 1.        ],\n",
       "       [0.98535856, 0.        ],\n",
       "       [0.99263352, 0.        ],\n",
       "       [0.92855573, 0.        ],\n",
       "       [0.99477217, 0.        ],\n",
       "       [0.21741175, 1.        ],\n",
       "       [0.94577689, 0.        ],\n",
       "       [0.97110875, 0.        ],\n",
       "       [0.98269296, 0.        ],\n",
       "       [0.49241096, 1.        ],\n",
       "       [0.98885707, 0.        ],\n",
       "       [0.9245476 , 0.        ],\n",
       "       [0.98678233, 0.        ],\n",
       "       [0.97397912, 0.        ],\n",
       "       [0.98912259, 0.        ],\n",
       "       [0.95420014, 0.        ],\n",
       "       [0.22714258, 1.        ],\n",
       "       [0.97807684, 0.        ],\n",
       "       [0.56740017, 0.        ],\n",
       "       [0.09688792, 1.        ],\n",
       "       [0.99632191, 0.        ],\n",
       "       [0.99227247, 0.        ],\n",
       "       [0.92802375, 0.        ],\n",
       "       [0.97366921, 0.        ],\n",
       "       [0.98369014, 0.        ],\n",
       "       [0.77011946, 0.        ],\n",
       "       [0.9962665 , 0.        ],\n",
       "       [0.99101979, 0.        ],\n",
       "       [0.42397342, 1.        ],\n",
       "       [0.99545552, 0.        ],\n",
       "       [0.97708153, 0.        ],\n",
       "       [0.99589451, 0.        ],\n",
       "       [0.30500285, 1.        ],\n",
       "       [0.69679063, 0.        ],\n",
       "       [0.36342535, 1.        ],\n",
       "       [0.97319446, 0.        ],\n",
       "       [0.9502447 , 0.        ],\n",
       "       [0.91317092, 0.        ],\n",
       "       [0.98010933, 0.        ],\n",
       "       [0.98335384, 0.        ],\n",
       "       [0.97425041, 0.        ],\n",
       "       [0.99473063, 0.        ],\n",
       "       [0.80075465, 0.        ],\n",
       "       [0.1874385 , 1.        ],\n",
       "       [0.9694313 , 0.        ],\n",
       "       [0.1817694 , 1.        ],\n",
       "       [0.96532622, 0.        ],\n",
       "       [0.97769722, 0.        ],\n",
       "       [0.97901427, 0.        ],\n",
       "       [0.4025175 , 1.        ],\n",
       "       [0.41049472, 1.        ],\n",
       "       [0.9878964 , 0.        ],\n",
       "       [0.33429489, 1.        ],\n",
       "       [0.99034863, 0.        ],\n",
       "       [0.96221801, 0.        ],\n",
       "       [0.57128286, 0.        ],\n",
       "       [0.97163106, 0.        ],\n",
       "       [0.99165225, 0.        ],\n",
       "       [0.62432537, 0.        ],\n",
       "       [0.98038881, 0.        ],\n",
       "       [0.96248005, 0.        ],\n",
       "       [0.97765441, 0.        ],\n",
       "       [0.97659761, 0.        ],\n",
       "       [0.95068896, 0.        ],\n",
       "       [0.61796641, 0.        ],\n",
       "       [0.44498097, 1.        ],\n",
       "       [0.98315702, 0.        ],\n",
       "       [0.85836257, 0.        ],\n",
       "       [0.98405361, 0.        ],\n",
       "       [0.61924121, 0.        ],\n",
       "       [0.96086226, 0.        ],\n",
       "       [0.33922648, 1.        ],\n",
       "       [0.17533858, 1.        ],\n",
       "       [0.9860757 , 0.        ],\n",
       "       [0.93877027, 0.        ],\n",
       "       [0.8970588 , 0.        ],\n",
       "       [0.97552559, 0.        ],\n",
       "       [0.9861327 , 0.        ],\n",
       "       [0.74139663, 0.        ],\n",
       "       [0.20381779, 1.        ],\n",
       "       [0.36505138, 1.        ],\n",
       "       [0.99630236, 0.        ],\n",
       "       [0.94267572, 0.        ],\n",
       "       [0.35874109, 1.        ],\n",
       "       [0.97618621, 0.        ],\n",
       "       [0.94047107, 0.        ],\n",
       "       [0.9677455 , 0.        ],\n",
       "       [0.12459769, 1.        ],\n",
       "       [0.96812122, 0.        ],\n",
       "       [0.45478732, 1.        ],\n",
       "       [0.47949333, 1.        ],\n",
       "       [0.10551235, 1.        ],\n",
       "       [0.39045305, 1.        ],\n",
       "       [0.98464751, 0.        ],\n",
       "       [0.97828629, 0.        ],\n",
       "       [0.19392701, 1.        ],\n",
       "       [0.98960895, 0.        ],\n",
       "       [0.99152288, 0.        ],\n",
       "       [0.96779296, 0.        ],\n",
       "       [0.79092235, 0.        ],\n",
       "       [0.47766672, 1.        ],\n",
       "       [0.89905476, 0.        ],\n",
       "       [0.40708233, 1.        ],\n",
       "       [0.93143965, 0.        ],\n",
       "       [0.97371649, 0.        ],\n",
       "       [0.99421258, 0.        ],\n",
       "       [0.98402028, 0.        ],\n",
       "       [0.94126302, 0.        ],\n",
       "       [0.99017404, 0.        ],\n",
       "       [0.98972558, 0.        ],\n",
       "       [0.9945232 , 0.        ],\n",
       "       [0.96642528, 0.        ],\n",
       "       [0.64750501, 0.        ],\n",
       "       [0.98347252, 0.        ],\n",
       "       [0.13875407, 1.        ],\n",
       "       [0.77190288, 0.        ],\n",
       "       [0.96256227, 0.        ],\n",
       "       [0.95999194, 0.        ],\n",
       "       [0.99047469, 0.        ],\n",
       "       [0.92984408, 0.        ],\n",
       "       [0.12136406, 1.        ],\n",
       "       [0.31241001, 1.        ],\n",
       "       [0.99147138, 0.        ],\n",
       "       [0.98284768, 0.        ],\n",
       "       [0.9918629 , 0.        ],\n",
       "       [0.9839395 , 0.        ],\n",
       "       [0.24758594, 1.        ],\n",
       "       [0.97825417, 0.        ],\n",
       "       [0.92227167, 0.        ],\n",
       "       [0.09776515, 1.        ],\n",
       "       [0.86675316, 0.        ],\n",
       "       [0.98899973, 0.        ],\n",
       "       [0.99031219, 0.        ],\n",
       "       [0.93010414, 0.        ],\n",
       "       [0.95933439, 0.        ],\n",
       "       [0.90403205, 0.        ],\n",
       "       [0.97504259, 0.        ],\n",
       "       [0.9888591 , 0.        ],\n",
       "       [0.9532283 , 0.        ],\n",
       "       [0.99687892, 0.        ],\n",
       "       [0.98822383, 0.        ],\n",
       "       [0.19130967, 1.        ],\n",
       "       [0.94750611, 0.        ],\n",
       "       [0.27779882, 1.        ],\n",
       "       [0.99573622, 0.        ],\n",
       "       [0.99405771, 0.        ],\n",
       "       [0.39720584, 1.        ],\n",
       "       [0.73660003, 0.        ],\n",
       "       [0.87515488, 0.        ],\n",
       "       [0.99703157, 0.        ],\n",
       "       [0.95358054, 0.        ],\n",
       "       [0.9540091 , 0.        ],\n",
       "       [0.33749937, 1.        ],\n",
       "       [0.89689979, 0.        ],\n",
       "       [0.91920717, 0.        ],\n",
       "       [0.09338631, 1.        ],\n",
       "       [0.88337284, 0.        ],\n",
       "       [0.9449683 , 0.        ],\n",
       "       [0.97238935, 0.        ],\n",
       "       [0.9702175 , 0.        ],\n",
       "       [0.98906026, 0.        ],\n",
       "       [0.69884472, 0.        ],\n",
       "       [0.48718967, 1.        ],\n",
       "       [0.97483423, 0.        ],\n",
       "       [0.93422396, 0.        ],\n",
       "       [0.96797292, 0.        ],\n",
       "       [0.98745364, 0.        ],\n",
       "       [0.85325201, 0.        ],\n",
       "       [0.93981441, 0.        ],\n",
       "       [0.98952166, 0.        ],\n",
       "       [0.94213433, 0.        ],\n",
       "       [0.98239271, 0.        ],\n",
       "       [0.62566016, 0.        ],\n",
       "       [0.98949326, 0.        ],\n",
       "       [0.9469001 , 0.        ],\n",
       "       [0.97759103, 0.        ],\n",
       "       [0.98875588, 0.        ],\n",
       "       [0.7526629 , 0.        ],\n",
       "       [0.64720557, 0.        ],\n",
       "       [0.6002454 , 0.        ],\n",
       "       [0.99214998, 0.        ],\n",
       "       [0.98770831, 0.        ],\n",
       "       [0.96155363, 0.        ],\n",
       "       [0.53008696, 0.        ],\n",
       "       [0.96020292, 0.        ],\n",
       "       [0.18658591, 1.        ],\n",
       "       [0.84787415, 0.        ],\n",
       "       [0.51812746, 0.        ],\n",
       "       [0.97407587, 0.        ],\n",
       "       [0.53947617, 0.        ],\n",
       "       [0.99358993, 0.        ],\n",
       "       [0.99069576, 0.        ],\n",
       "       [0.97281209, 0.        ],\n",
       "       [0.92382394, 0.        ],\n",
       "       [0.97486312, 0.        ],\n",
       "       [0.99305568, 0.        ],\n",
       "       [0.96574677, 0.        ],\n",
       "       [0.16042789, 1.        ],\n",
       "       [0.94577431, 0.        ],\n",
       "       [0.98366854, 0.        ],\n",
       "       [0.86967606, 0.        ],\n",
       "       [0.52125992, 0.        ],\n",
       "       [0.96446876, 0.        ],\n",
       "       [0.96589064, 0.        ],\n",
       "       [0.98863881, 0.        ],\n",
       "       [0.06138454, 1.        ],\n",
       "       [0.99076627, 0.        ],\n",
       "       [0.94080204, 0.        ],\n",
       "       [0.95506406, 0.        ],\n",
       "       [0.95790595, 0.        ],\n",
       "       [0.55443743, 0.        ],\n",
       "       [0.60041146, 0.        ],\n",
       "       [0.99646756, 0.        ],\n",
       "       [0.43631934, 1.        ],\n",
       "       [0.92903654, 0.        ],\n",
       "       [0.46925698, 1.        ],\n",
       "       [0.38886662, 1.        ],\n",
       "       [0.99549108, 0.        ],\n",
       "       [0.17269323, 1.        ],\n",
       "       [0.53560638, 0.        ],\n",
       "       [0.98326123, 0.        ],\n",
       "       [0.3517642 , 1.        ],\n",
       "       [0.99040701, 0.        ],\n",
       "       [0.988392  , 0.        ],\n",
       "       [0.99705339, 0.        ],\n",
       "       [0.96211842, 0.        ],\n",
       "       [0.9476318 , 0.        ]])"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## 6 结果输出\n",
    "## 6.1 test集的预测\n",
    "# y_test_pred = gbm.predict_proba(test.drop(['fraud'],axis=1))\n",
    "# y_test_pred[:, 1][y_test_pred[:, 1] > 0.5] = '1'\n",
    "# y_test_pred[:, 1][y_test_pred[:, 1] <= 0.5] = '0'\n",
    "# y_test_pred"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 6.2 输出结果\n",
    "\n",
    "# result = pd.read_csv('./data/submission.csv')\n",
    "# result['fraud'] = y_test_pred[:, 1]\n",
    "# result.to_csv(f'./data/{model_name}.csv', index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>model_name</th>\n",
       "      <th>update_time</th>\n",
       "      <th>Accuracy</th>\n",
       "      <th>Precision</th>\n",
       "      <th>Recall</th>\n",
       "      <th>F1 Score</th>\n",
       "      <th>auc</th>\n",
       "      <th>sub_score</th>\n",
       "      <th>update_content</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Empty DataFrame\n",
       "Columns: [model_name, update_time, Accuracy, Precision, Recall, F1 Score, auc, sub_score, update_content]\n",
       "Index: []"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## 7 模型评估结果输出\n",
    "# evalue_result=pd.read_csv('./data/evalue_result.csv', encoding='utf-8')\n",
    "# evalue_result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "## 5.1 模型评估结果输出\n",
    "# import datetime \n",
    "\n",
    "# new_row = {'model_name': model_name, 'update_time': datetime.datetime.now() , 'Accuracy': accuracy, 'Precision': precision, 'Recall': recall\n",
    "#  , 'F1 Score': f1, 'auc': auc, 'sub_score': 0.8496, 'update_content': 'base model'}  \n",
    "# evalue_result.loc[len(evalue_result.index)] = new_row \n",
    "# evalue_result\n",
    "# evalue_result.to_csv('./data/evalue_result.csv', index=False)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
